package com.geeklei.media.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

import com.geeklei.media.ui.common.DisplayInformationFetcher;


/**
 * Created by leixun on 16/5/31.
 */
public class ControllerView extends RelativeLayout {
    public static final String TAG = "ControllerView";
    private static float X_TOLERANCE = 0.0f;
    private static float Y_TOLERANCE = 0.0f;
    private DisplayInformationFetcher mDisplayInformationFetcher;
    private DisplayMetrics mDisplayMetrics;
    private boolean mDownLeftRegion = false;
    private boolean mDownRightRegion = false;
    private OnControlEventListener mGestureListener;
    private boolean mMoved = false;
    private boolean mMovedCenter = false;
    private boolean mMovedLeft = false;
    private boolean mMovedRight = false;
    private float mTouchStartY = -1.0f;
    private float mX = 0.0f;
    private float mY = 0.0f;

    public interface OnControlEventListener {
        public static final int REGION_CENTER = 2;
        public static final int REGION_LEFT = 0;
        public static final int REGION_RIGHT = 1;

        void onTap(int i);

        void onTouchMove(int i, float f, float f2);

        void onTouchUp(int i);
    }

    public ControllerView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        init();
    }

    public ControllerView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        init();
    }

    public ControllerView(Context context) {
        super(context);
        init();
    }

    private void init() {
        this.mDisplayInformationFetcher = DisplayInformationFetcher.getInstance(getContext().getApplicationContext());
        this.mDisplayMetrics = this.mDisplayInformationFetcher.getDisplayMetrics();
        Y_TOLERANCE = this.mDisplayMetrics.density * 10.0f;
        X_TOLERANCE = this.mDisplayMetrics.density * 10.0f;
        setFocusable(true);
        setFocusableInTouchMode(true);
        setClickable(true);
    }

    public void setGestureListener(OnControlEventListener onControlEventListener) {
        this.mGestureListener = onControlEventListener;
    }

    @SuppressLint({"ClickableViewAccessibility"})
    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (!isClickable()) {
            return false;
        }
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            this.mTouchStartY = motionEvent.getY();
            touchStart(motionEvent.getX(), motionEvent.getY());
        } else if (this.mTouchStartY >= 0.0f && this.mTouchStartY < ((float) 10)) {
            return false;
        } else {
            if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
                this.mTouchStartY = -1.0f;
                touchMove(motionEvent.getX(), motionEvent.getY());
            } else if (motionEvent.getAction() == MotionEvent.ACTION_UP || motionEvent.getAction() == MotionEvent.ACTION_CANCEL) {
                touchUp(motionEvent.getX(), motionEvent.getY());
                this.mTouchStartY = -1.0f;
            }
        }
        return true;
    }

    private void touchStart(float getX, float getY) {
        int screenWidth = this.mDisplayInformationFetcher.getScreenWidth();
        this.mX = getX;
        this.mY = getY;
        this.mMovedLeft = false;
        this.mMovedRight = false;
        this.mMovedCenter = false;
        if (this.mX <= ((float) (screenWidth / 2))) {
            this.mDownLeftRegion = true;
        } else if (this.mX >= ((float) (screenWidth - (screenWidth / 2)))) {
            this.mDownRightRegion = true;
        }
    }

    private void touchMove(float getX, float getY) {
        float f3 = getX - this.mX;
        float f4 = getY - this.mY;
        float abs = Math.abs(f3);
        float abs2 = Math.abs(f4);
        if (abs2 > Y_TOLERANCE && ((abs2 > abs || this.mMovedLeft || this.mMovedRight) && !this.mMovedCenter)) {
            if (this.mDownLeftRegion) {
                this.mMovedLeft = true;
                if (this.mGestureListener != null) {
                    this.mGestureListener.onTouchMove(OnControlEventListener.REGION_LEFT, f3, f4);
                }
                this.mMoved = true;
                this.mX = getX;
                this.mY = getY;
            }
            if (this.mDownRightRegion) {
                this.mMovedRight = true;
                if (this.mGestureListener != null) {
                    this.mGestureListener.onTouchMove(OnControlEventListener.REGION_RIGHT, f3, f4);
                }
                this.mMoved = true;
                this.mX = getX;
                this.mY = getY;
            }
        } else if (abs <= X_TOLERANCE) {
        } else {
            if ((abs >= abs2 || this.mMovedCenter) && !this.mMovedLeft && !this.mMovedRight) {
                this.mMovedCenter = true;
                if (this.mGestureListener != null) {
                    this.mGestureListener.onTouchMove(OnControlEventListener.REGION_CENTER, f3, f4);
                }
                this.mMoved = true;
            }
        }
    }

    private void touchUp(float lastX, float lastY) {
        if (this.mMoved) {
            if (this.mGestureListener != null) {
                if (this.mMovedLeft) {
                    this.mGestureListener.onTouchUp(OnControlEventListener.REGION_LEFT);
                } else if (this.mMovedRight) {
                    this.mGestureListener.onTouchUp(OnControlEventListener.REGION_RIGHT);
                } else if (this.mMovedCenter) {
                    this.mGestureListener.onTouchUp(OnControlEventListener.REGION_CENTER);
                }
            }
        } else if (this.mGestureListener != null) {
            this.mGestureListener.onTap(OnControlEventListener.REGION_CENTER);
        }
        this.mMoved = false;
        this.mDownLeftRegion = false;
        this.mDownRightRegion = false;
    }
}
